package com.handpay.nocardpay.baselibrary.pay.uniopayquick.view;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.pay.uniopayquick.model.bean.UnionQuickPayRespBean;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;

/**
 * @author sxshi
 *         交易成功界面
 */
public class TransactionResultsSuccessFragment extends BaseFragment {

    private ImageView iv_tradeType;
    private TextView tv_amount;
    private TextView tv_shopname;
    private TextView tv_transTime;
    private TextView tv_cardNO;
    private TextView tv_sersNo;

    private UnionQuickPayRespBean mQuickPayRespBean;
    private final String ZERO = "0";

    public static TransactionResultsSuccessFragment newInstance(UnionQuickPayRespBean quickPayRespBean) {

        Bundle args = new Bundle();
        args.putParcelable(BaseProtocolKey.QUICKPAYRSP, quickPayRespBean);
        TransactionResultsSuccessFragment fragment = new TransactionResultsSuccessFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_result_success;
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getResources().getString(R.string.trans_result);
    }

    @Override
    public void initView(View view) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(toolbar, true);
        iv_tradeType = (ImageView) view.findViewById(R.id.iv_tradeType);
        tv_amount = (TextView) view.findViewById(R.id.tv_amount);
        tv_shopname = (TextView) view.findViewById(R.id.tv_shopname);
        tv_transTime = (TextView) view.findViewById(R.id.tv_transTime);
        tv_cardNO = (TextView) view.findViewById(R.id.tv_cardNO);
        tv_sersNo = (TextView) view.findViewById(R.id.tv_sersNo);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mQuickPayRespBean = getArguments().getParcelable(BaseProtocolKey.QUICKPAYRSP);
        if (mQuickPayRespBean != null) {
            tv_amount.setText(String.format(_mActivity.getString(R.string.mine_money), CommonUtils.formatMoney(Double.parseDouble(SecureManager.getInstance().des(mQuickPayRespBean.getAmount(), 0, null)) / 100)));
            tv_transTime.setText(CommonUtils.formatDateToString(CommonUtils
                    .formatStringToDate(mQuickPayRespBean.getTransTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
            tv_shopname.setText(SecureManager.getInstance().des(mQuickPayRespBean.getMerchantcode(), 0, null));
            tv_cardNO.setText(CommonUtils.formattingCard(SecureManager.getInstance().des(mQuickPayRespBean.getTransAccount(), 0, null)));
            tv_sersNo.setText(mQuickPayRespBean.getTransSN());

            if (!(ZERO.equals(mQuickPayRespBean.getPrizeTotalNum()) && ZERO.equals(mQuickPayRespBean.getPrizeTotalAmount()))) {
                String prizeTotalNum = String.format(_mActivity.getString(R.string.lottery_num), mQuickPayRespBean.getPrizeTotalNum());
                String prizeTotalAmount = String.format(_mActivity.getString(R.string.lottery_lees_amount), Double.parseDouble(mQuickPayRespBean.getPrizeTotalAmount()) / 100);
                if ((Integer.valueOf(mQuickPayRespBean.getPrizeTotalNum())) > 0) {
                    DialogUtils.showAlertDialog(_mActivity, _mActivity.getResources().getString(R.string.tip), prizeTotalNum, false);
                } else {
                    DialogUtils.showAlertDialog(_mActivity, _mActivity.getResources().getString(R.string.tip), prizeTotalAmount, false);
                }
            }
        }
    }
}
